/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sport;

import sport.*;
import java.util.Date;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.LoginScreen;
import sport.media.BellFactory;
import sport.media.IBell;
/**
 * @author 7904112B
 */
public class BBeepMidlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    
    private Settings cfg;
    private IBell bell;

//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
private java.util.Hashtable __previousDisplayables = new java.util.Hashtable ();
private Alert alertWait;
private Form frmMain;
private TextField tfWork;
private TextField tfLaps;
private TextField tfRest;
private Alert alertAbout;
private Form frmBtSend;
private Form frmConfig;
private ChoiceGroup cgBell;
private ChoiceGroup cgAlert;
private TextField tfDelay;
private Alert erreurSaisie;
private LoginScreen loginScreen;
private Command cmdAbout;
private Command cmdStart;
private Command cmdStop;
private Command cmdSend;
private Command cmdConfig;
private Command backCommand;
private Command cmdOk;
private Command exitCommand;
//</editor-fold>//GEN-END:|fields|0|

    /**
     * The BBeepMidlet constructor.
     */
    public BBeepMidlet() {
        cfg  = new Settings();
        bell = BellFactory.getBell(cfg.BELL);
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
/**
 * Switches a display to previous displayable of the current displayable.
 * The <code>display</code> instance is obtain from the <code>getDisplay</code> method.
 */
private void switchToPreviousDisplayable () {
Displayable __currentDisplayable = getDisplay ().getCurrent ();
if (__currentDisplayable != null) {
Displayable __nextDisplayable = (Displayable) __previousDisplayables.get (__currentDisplayable);
if (__nextDisplayable != null) {
switchDisplayable (null, __nextDisplayable);
}
}
}
//</editor-fold>//GEN-END:|methods|0|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
/**
 * Initilizes the application.
 * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
 */
private void initialize () {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
}//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
/**
 * Performs an action assigned to the Mobile Device - MIDlet Started point.
 */
public void startMIDlet () {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
switchDisplayable (null, getFrmMain ());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
        getFrmMain().removeCommand(cmdStop);
}//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
/**
 * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
 */
public void resumeMIDlet () {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
}//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
/**
 * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
 * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
 * @param nextDisplayable the Displayable to be set
 */
public void switchDisplayable (Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
Display display = getDisplay ();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
Displayable __currentDisplayable = display.getCurrent ();
if (__currentDisplayable != null  &&  nextDisplayable != null) {
__previousDisplayables.put (nextDisplayable, __currentDisplayable);
}
if (alert == null) {
display.setCurrent (nextDisplayable);
} else {
display.setCurrent (alert, nextDisplayable);
}//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
}//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmMain ">//GEN-BEGIN:|13-getter|0|13-preInit
/**
 * Returns an initiliazed instance of frmMain component.
 * @return the initialized component instance
 */
public Form getFrmMain () {
if (frmMain == null) {//GEN-END:|13-getter|0|13-preInit
            // write pre-init user code here
frmMain = new Form ("BBeep!", new Item[] { getTfWork (), getTfRest (), getTfLaps () });//GEN-BEGIN:|13-getter|1|13-postInit
frmMain.addCommand (getCmdStart ());
frmMain.addCommand (getCmdStop ());
frmMain.addCommand (getCmdAbout ());
frmMain.addCommand (getCmdConfig ());
frmMain.addCommand (getExitCommand ());
frmMain.setCommandListener (this);//GEN-END:|13-getter|1|13-postInit
            // write post-init user code here
}//GEN-BEGIN:|13-getter|2|
return frmMain;
}
//</editor-fold>//GEN-END:|13-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
/**
 * Called by a system to indicated that a command has been invoked on a particular displayable.
 * @param command the Command that was invoked
 * @param displayable the Displayable where the command was invoked
 */
public void commandAction (Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
if (displayable == frmBtSend) {//GEN-BEGIN:|7-commandAction|1|173-preAction
if (command == backCommand) {//GEN-END:|7-commandAction|1|173-preAction
                // write pre-action user code here
switchToPreviousDisplayable ();//GEN-LINE:|7-commandAction|2|173-postAction
                // write post-action user code here
}//GEN-BEGIN:|7-commandAction|3|42-preAction
} else if (displayable == frmConfig) {
if (command == cmdOk) {//GEN-END:|7-commandAction|3|42-preAction
                // write pre-action user code here
saveConfig ();//GEN-LINE:|7-commandAction|4|42-postAction
                // write post-action user code here
               
}//GEN-BEGIN:|7-commandAction|5|132-preAction
} else if (displayable == frmMain) {
if (command == cmdAbout) {//GEN-END:|7-commandAction|5|132-preAction
                // write pre-action user code here
switchDisplayable (getAlertAbout (), getFrmMain ());//GEN-LINE:|7-commandAction|6|132-postAction
                // write post-action user code here
} else if (command == cmdConfig) {//GEN-LINE:|7-commandAction|7|39-preAction
                // write pre-action user code here
switchDisplayable (null, getFrmConfig ());//GEN-LINE:|7-commandAction|8|39-postAction
                // write post-action user code here
} else if (command == cmdStart) {//GEN-LINE:|7-commandAction|9|21-preAction
                // write pre-action user code here
countdown();//GEN-LINE:|7-commandAction|10|21-postAction
                // write post-action user code here
} else if (command == cmdStop) {//GEN-LINE:|7-commandAction|11|31-preAction
                // write pre-action user code here
RUN=false;//GEN-BEGIN:|7-commandAction|12|31-postAction
//try{counter.notify();}catch (Exception e){}//GEN-END:|7-commandAction|12|31-postAction
                // write post-action user code here
} else if (command == exitCommand) {//GEN-LINE:|7-commandAction|13|124-preAction
                // write pre-action user code here
exitMIDlet ();//GEN-LINE:|7-commandAction|14|124-postAction
                // write post-action user code here
}//GEN-BEGIN:|7-commandAction|15|183-preAction
} else if (displayable == loginScreen) {
if (command == LoginScreen.LOGIN_COMMAND) {//GEN-END:|7-commandAction|15|183-preAction
 // write pre-action user code here
//GEN-LINE:|7-commandAction|16|183-postAction
 // write post-action user code here
}//GEN-BEGIN:|7-commandAction|17|7-postCommandAction
}//GEN-END:|7-commandAction|17|7-postCommandAction
        // write post-action user code here
}//GEN-BEGIN:|7-commandAction|18|
//</editor-fold>//GEN-END:|7-commandAction|18|


//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfWork ">//GEN-BEGIN:|17-getter|0|17-preInit
/**
 * Returns an initiliazed instance of tfWork component.
 * @return the initialized component instance
 */
public TextField getTfWork () {
if (tfWork == null) {//GEN-END:|17-getter|0|17-preInit
            // write pre-init user code here
tfWork = new TextField ("Travail", formatTime(cfg.WORK), 5, TextField.DECIMAL);//GEN-BEGIN:|17-getter|1|17-postInit
tfWork.setLayout (ImageItem.LAYOUT_DEFAULT);
tfWork.setPreferredSize (-1, -1);//GEN-END:|17-getter|1|17-postInit
            // write post-init user code here
}//GEN-BEGIN:|17-getter|2|
return tfWork;
}
//</editor-fold>//GEN-END:|17-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfRest ">//GEN-BEGIN:|18-getter|0|18-preInit
/**
 * Returns an initiliazed instance of tfRest component.
 * @return the initialized component instance
 */
public TextField getTfRest () {
if (tfRest == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
tfRest = new TextField ("Repos", formatTime(cfg.REST), 5, TextField.DECIMAL);//GEN-BEGIN:|18-getter|1|18-postInit
tfRest.setLayout (ImageItem.LAYOUT_DEFAULT | Item.LAYOUT_SHRINK | Item.LAYOUT_2);
tfRest.setPreferredSize (-1, -1);//GEN-END:|18-getter|1|18-postInit
            // write post-init user code here
}//GEN-BEGIN:|18-getter|2|
return tfRest;
}
//</editor-fold>//GEN-END:|18-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfLaps ">//GEN-BEGIN:|19-getter|0|19-preInit
/**
 * Returns an initiliazed instance of tfLaps component.
 * @return the initialized component instance
 */
public TextField getTfLaps () {
if (tfLaps == null) {//GEN-END:|19-getter|0|19-preInit
            // write pre-init user code here
tfLaps = new TextField ("Rounds", String.valueOf(cfg.LAPS), 2, TextField.NUMERIC);//GEN-BEGIN:|19-getter|1|19-postInit
tfLaps.setLayout (ImageItem.LAYOUT_DEFAULT);//GEN-END:|19-getter|1|19-postInit
            // write post-init user code here
}//GEN-BEGIN:|19-getter|2|
return tfLaps;
}
//</editor-fold>//GEN-END:|19-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdStart ">//GEN-BEGIN:|20-getter|0|20-preInit
/**
 * Returns an initiliazed instance of cmdStart component.
 * @return the initialized component instance
 */
public Command getCmdStart () {
if (cmdStart == null) {//GEN-END:|20-getter|0|20-preInit
            // write pre-init user code here
cmdStart = new Command ("Start", Command.SCREEN, 5);//GEN-LINE:|20-getter|1|20-postInit
            // write post-init user code here
}//GEN-BEGIN:|20-getter|2|
return cmdStart;
}
//</editor-fold>//GEN-END:|20-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdStop ">//GEN-BEGIN:|30-getter|0|30-preInit
/**
 * Returns an initiliazed instance of cmdStop component.
 * @return the initialized component instance
 */
public Command getCmdStop () {
if (cmdStop == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
cmdStop = new Command ("Stop", Command.SCREEN, 5);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
}//GEN-BEGIN:|30-getter|2|
return cmdStop;
}
//</editor-fold>//GEN-END:|30-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmConfig ">//GEN-BEGIN:|37-getter|0|37-preInit
/**
 * Returns an initiliazed instance of frmConfig component.
 * @return the initialized component instance
 */
public Form getFrmConfig () {
if (frmConfig == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
frmConfig = new Form ("R\u00E9glages", new Item[] { getTfDelay (), getCgAlert (), getCgBell () });//GEN-BEGIN:|37-getter|1|37-postInit
frmConfig.addCommand (getCmdOk ());
frmConfig.setCommandListener (this);//GEN-END:|37-getter|1|37-postInit
            // write post-init user code here
}//GEN-BEGIN:|37-getter|2|
return frmConfig;
}
//</editor-fold>//GEN-END:|37-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdConfig ">//GEN-BEGIN:|38-getter|0|38-preInit
/**
 * Returns an initiliazed instance of cmdConfig component.
 * @return the initialized component instance
 */
public Command getCmdConfig () {
if (cmdConfig == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
cmdConfig = new Command ("R\u00E9glages", Command.HELP, 2);//GEN-LINE:|38-getter|1|38-postInit
            // write post-init user code here
}//GEN-BEGIN:|38-getter|2|
return cmdConfig;
}
//</editor-fold>//GEN-END:|38-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdOk ">//GEN-BEGIN:|41-getter|0|41-preInit
/**
 * Returns an initiliazed instance of cmdOk component.
 * @return the initialized component instance
 */
public Command getCmdOk () {
if (cmdOk == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
cmdOk = new Command ("Ok", Command.OK, 0);//GEN-LINE:|41-getter|1|41-postInit
            // write post-init user code here
}//GEN-BEGIN:|41-getter|2|
return cmdOk;
}
//</editor-fold>//GEN-END:|41-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfDelay ">//GEN-BEGIN:|82-getter|0|82-preInit
/**
 * Returns an initiliazed instance of tfDelay component.
 * @return the initialized component instance
 */
public TextField getTfDelay () {
if (tfDelay == null) {//GEN-END:|82-getter|0|82-preInit
tfDelay = new TextField ("Diff\u00E9r\u00E9 (<5s)", String.valueOf(cfg.DELAY), 1, TextField.NUMERIC);//GEN-LINE:|82-getter|1|82-postInit
            // write post-init user code here
}//GEN-BEGIN:|82-getter|2|
return tfDelay;
}
//</editor-fold>//GEN-END:|82-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cgAlert ">//GEN-BEGIN:|83-getter|0|83-preInit
/**
 * Returns an initiliazed instance of cgAlert component.
 * @return the initialized component instance
 */
public ChoiceGroup getCgAlert () {
if (cgAlert == null) {//GEN-END:|83-getter|0|83-preInit
            // write pre-init user code here
cgAlert = new ChoiceGroup ("Alerte", Choice.MULTIPLE);//GEN-BEGIN:|83-getter|1|83-postInit
cgAlert.setPreferredSize (-1, -1);
cgAlert.append ("Vibreur", null);
cgAlert.append ("Flash", null);
cgAlert.setLayout (ImageItem.LAYOUT_DEFAULT);
cgAlert.setFitPolicy (Choice.TEXT_WRAP_DEFAULT);
cgAlert.setSelectedFlags (new boolean[] { cfg.VIBRATE, cfg.FLASH });
cgAlert.setFont (0, null);
cgAlert.setFont (1, null);//GEN-END:|83-getter|1|83-postInit
            // write post-init user code here
}//GEN-BEGIN:|83-getter|2|
return cgAlert;
}
//</editor-fold>//GEN-END:|83-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cgBell ">//GEN-BEGIN:|86-getter|0|86-preInit
/**
 * Returns an initiliazed instance of cgBell component.
 * @return the initialized component instance
 */
public ChoiceGroup getCgBell () {
if (cgBell == null) {//GEN-END:|86-getter|0|86-preInit
            // write pre-init user code here
cgBell = new ChoiceGroup ("Bell type", Choice.EXCLUSIVE);//GEN-BEGIN:|86-getter|1|86-postInit
cgBell.append ("Boxing bell", null);
cgBell.append ("Beep", null);
cgBell.setSelectedFlags (new boolean[] { cfg.BELL == BellFactory.MMAPISampled, cfg.BELL == BellFactory.MMAPIBell });
cgBell.setFont (0, null);
cgBell.setFont (1, null);//GEN-END:|86-getter|1|86-postInit
            // write post-init user code here
}//GEN-BEGIN:|86-getter|2|
return cgBell;
}
//</editor-fold>//GEN-END:|86-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: saveConfig ">//GEN-BEGIN:|106-entry|0|107-preAction
/**
 * Performs an action assigned to the saveConfig entry-point.
 */
public void saveConfig () {//GEN-END:|106-entry|0|107-preAction
        int i;
        try {i= Integer.parseInt(getTfDelay().getString());}
        catch (NumberFormatException e) { i=0; e=null;}
        cfg.DELAY   = i>5 ? 5 : i;
        cfg.VIBRATE = getCgAlert().isSelected(0);
        cfg.FLASH   = getCgAlert().isSelected(1);
        cfg.BELL    = getCgBell().getSelectedIndex();
        cfg.Save();
        // write pre-action user code here
        bell = BellFactory.getBell(cfg.BELL);
switchToPreviousDisplayable ();//GEN-LINE:|106-entry|1|107-postAction
        // write post-action user code here
}//GEN-BEGIN:|106-entry|2|
//</editor-fold>//GEN-END:|106-entry|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|123-getter|0|123-preInit
/**
 * Returns an initiliazed instance of exitCommand component.
 * @return the initialized component instance
 */
public Command getExitCommand () {
if (exitCommand == null) {//GEN-END:|123-getter|0|123-preInit
            // write pre-init user code here
exitCommand = new Command ("Quitter", Command.EXIT, 1);//GEN-LINE:|123-getter|1|123-postInit
            // write post-init user code here
}//GEN-BEGIN:|123-getter|2|
return exitCommand;
}
//</editor-fold>//GEN-END:|123-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: alertWait ">//GEN-BEGIN:|129-getter|0|129-preInit
/**
 * Returns an initiliazed instance of alertWait component.
 * @return the initialized component instance
 */
public Alert getAlertWait () {
if (alertWait == null) {//GEN-END:|129-getter|0|129-preInit
            // write pre-init user code here
alertWait = new Alert ("D\u00E9part diff\u00E9r\u00E9", "La compteur va démarrer dans " + String.valueOf(cfg.DELAY) + "s...", null, AlertType.INFO);//GEN-BEGIN:|129-getter|1|129-postInit
alertWait.setTimeout ((cfg.DELAY * SECOND));//GEN-END:|129-getter|1|129-postInit
            // write post-init user code here
}//GEN-BEGIN:|129-getter|2|
return alertWait;
}
//</editor-fold>//GEN-END:|129-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdAbout ">//GEN-BEGIN:|131-getter|0|131-preInit
/**
 * Returns an initiliazed instance of cmdAbout component.
 * @return the initialized component instance
 */
public Command getCmdAbout () {
if (cmdAbout == null) {//GEN-END:|131-getter|0|131-preInit
            // write pre-init user code here
cmdAbout = new Command ("A propos de", Command.HELP, 3);//GEN-LINE:|131-getter|1|131-postInit
            // write post-init user code here
}//GEN-BEGIN:|131-getter|2|
return cmdAbout;
}
//</editor-fold>//GEN-END:|131-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: alertAbout ">//GEN-BEGIN:|152-getter|0|152-preInit
/**
 * Returns an initiliazed instance of alertAbout component.
 * @return the initialized component instance
 */
public Alert getAlertAbout () {
if (alertAbout == null) {//GEN-END:|152-getter|0|152-preInit
            // write pre-init user code here
alertAbout = new Alert ("BBeep! v1.0", "D\u00E9velopp\u00E9 par xavier.delestre@free.fr.\n", null, null);//GEN-BEGIN:|152-getter|1|152-postInit
alertAbout.setTimeout (Alert.FOREVER);//GEN-END:|152-getter|1|152-postInit
            // write post-init user code here
}//GEN-BEGIN:|152-getter|2|
return alertAbout;
}
//</editor-fold>//GEN-END:|152-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: erreurSaisie ">//GEN-BEGIN:|162-getter|0|162-preInit
/**
 * Returns an initiliazed instance of erreurSaisie component.
 * @return the initialized component instance
 */
public Alert getErreurSaisie () {
if (erreurSaisie == null) {//GEN-END:|162-getter|0|162-preInit
            // write pre-init user code here
erreurSaisie = new Alert ("Erreur de saisie", "Formats :\nTemps : mm.ss ou ss\nRing : 0 \u00E0 99", null, AlertType.ERROR);//GEN-BEGIN:|162-getter|1|162-postInit
erreurSaisie.setTimeout (Alert.FOREVER);//GEN-END:|162-getter|1|162-postInit
            // write post-init user code here
}//GEN-BEGIN:|162-getter|2|
return erreurSaisie;
}
//</editor-fold>//GEN-END:|162-getter|2|


    
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmBtSend ">//GEN-BEGIN:|171-getter|0|171-preInit
/**
 * Returns an initiliazed instance of frmBtSend component.
 * @return the initialized component instance
 */
public Form getFrmBtSend () {
if (frmBtSend == null) {//GEN-END:|171-getter|0|171-preInit
            // write pre-init user code here
frmBtSend = new Form ("Appareils d\u00E9tect\u00E9s");//GEN-BEGIN:|171-getter|1|171-postInit
frmBtSend.addCommand (getBackCommand ());
frmBtSend.setCommandListener (this);//GEN-END:|171-getter|1|171-postInit
            // write post-init user code here
}//GEN-BEGIN:|171-getter|2|
return frmBtSend;
}
//</editor-fold>//GEN-END:|171-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSend ">//GEN-BEGIN:|168-getter|0|168-preInit
/**
 * Returns an initiliazed instance of cmdSend component.
 * @return the initialized component instance
 */
public Command getCmdSend () {
if (cmdSend == null) {//GEN-END:|168-getter|0|168-preInit
            // write pre-init user code here
cmdSend = new Command ("Envoyer", Command.HELP, 0);//GEN-LINE:|168-getter|1|168-postInit
            // write post-init user code here
}//GEN-BEGIN:|168-getter|2|
return cmdSend;
}
//</editor-fold>//GEN-END:|168-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|172-getter|0|172-preInit
/**
 * Returns an initiliazed instance of backCommand component.
 * @return the initialized component instance
 */
public Command getBackCommand () {
if (backCommand == null) {//GEN-END:|172-getter|0|172-preInit
            // write pre-init user code here
backCommand = new Command ("Back", Command.BACK, 0);//GEN-LINE:|172-getter|1|172-postInit
            // write post-init user code here
}//GEN-BEGIN:|172-getter|2|
return backCommand;
}
//</editor-fold>//GEN-END:|172-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: loginScreen ">//GEN-BEGIN:|181-getter|0|181-preInit
/**
 * Returns an initiliazed instance of loginScreen component.
 * @return the initialized component instance
 */
public LoginScreen getLoginScreen () {
if (loginScreen == null) {//GEN-END:|181-getter|0|181-preInit
 // write pre-init user code here
loginScreen = new LoginScreen (getDisplay ());//GEN-BEGIN:|181-getter|1|181-postInit
loginScreen.setLabelTexts ("Username", "Password");
loginScreen.setTitle ("loginScreen");
loginScreen.addCommand (LoginScreen.LOGIN_COMMAND);
loginScreen.setCommandListener (this);
loginScreen.setBGColor (-3355444);
loginScreen.setFGColor (0);
loginScreen.setUseLoginButton (false);
loginScreen.setLoginButtonText ("Login");//GEN-END:|181-getter|1|181-postInit
 // write post-init user code here
}//GEN-BEGIN:|181-getter|2|
return loginScreen;
}
//</editor-fold>//GEN-END:|181-getter|2|













    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        cfg.WORK = formatTime( getTfWork().getString() );
        cfg.REST = formatTime( getTfRest().getString() );
        cfg.LAPS = Integer.parseInt( getTfLaps().getString() );
        cfg.Save();
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    public final int SECOND=1000;
    public Date formatTime(String t)
    {   
        Date date = new Date();
        int idx = t.indexOf(".");
        
        if (idx == -1)
            date.setTime(Integer.parseInt(t)*SECOND);
        else
        {
            int min = Integer.parseInt( t.substring(0,idx) );
            int sec = Integer.parseInt( t.substring(idx+1,t.length()) );
            date.setTime(((min * 60) + sec) * SECOND);
        }
        
        return date;        
    }

    public String formatTime(long l)
    {
        return formatTime(new Date(l));
    }
    public String formatTime(Date d)
    {
        String s;
        //time longer than 60 secs : 59.59 format
        if (d.getTime()>60000)
        {
            s = d.toString();
            int p = s.indexOf(":");
            s=s.substring(p+1,p+6).replace(':','.');
             //time shorter than 10mins : 9.59 format
             if (s.startsWith("0")) s = s.substring(1) ;
        }
        //time shorter than 60 secs : 59 format
        else
            s = Long.toString(d.getTime()/1000);
        
        return  s;
       /* StringBuffer sb = new StringBuffer();
        sb.append(c.get(CalendarImpl.MINUTE));
        sb.append(".");
        sb.append(c.get(CalendarImpl.SECOND));   */
    }

    
    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
        cfg.REST = workTime;
    }
    
    public boolean RUN = true; 
    public static Thread counter = null;
    
     
    Date workTime ;
    Date restTime ;
    int laps      ;

    private void countdown()
    {        
                try
                {
                    workTime = formatTime(tfWork.getString());
                    restTime = formatTime(tfRest.getString());
                    laps     = Integer.parseInt(tfLaps.getString());
                }
                catch(NumberFormatException e)
                {                    
                    switchDisplayable(null, getErreurSaisie());
                    e=null;
                    return;
                }
                
                //Start form
                getFrmMain().removeCommand(cmdStart);
                getFrmMain().addCommand(cmdStop);

                tfWork.setString(formatTime(workTime));
                tfRest.setString(formatTime(restTime));
 
                
                if (cfg.DELAY>0) switchDisplayable(null, getAlertWait());

                counter = new Thread() 
                {
                    public void run() {
                    RUN = true; 
                        
                    //DELAYED START
                    try { 
                        //switchDisplayable(getAlertWait(), getFrmMain());
                        Thread.sleep(cfg.DELAY * SECOND); 
                    } 
                    catch (Exception e) {}
                        
                    //FOR EACH LAP
                    for (int currentLap = laps; currentLap>0 && RUN ;currentLap-- )
                    {
                            //Update field with remaining lap
                            tfLaps.setString(String.valueOf(currentLap));

                            //Start Work : sould rings once
                            bell.ringsStart();

                            //WORK
                            for (long currentWork = workTime.getTime(); currentWork > 0 && RUN;currentWork -= SECOND) 
                            {
                                    tfWork.setString( formatTime(currentWork));
                                if (currentWork < 4*SECOND) getDisplay().flashBacklight(250);
                                try {
                                    Thread.sleep(SECOND);
                                } catch (InterruptedException ie) {
                                    ie.printStackTrace();
                                }

                            }
                            tfWork.setString(formatTime(0));


                            //REST
                            //if (currentLap>1) //Skip rest if we're in the last lap
                            for (long current = restTime.getTime(); current > 0 && RUN ;current-=SECOND) 
                            {
                                //begin of rest : should ring twice
                                if (current == restTime.getTime()) bell.ringsRest();
                                
                                tfRest.setString( formatTime(current));

                                if (current < 4*SECOND) getDisplay().flashBacklight(250);
                                try {
                                    Thread.sleep(SECOND);
                                } catch (InterruptedException ie) {
                                    ie.printStackTrace();
                                }
                            }
                            tfRest.setString(formatTime(0));
                            
                    }
                    tfLaps.setString(String.valueOf(laps));
                    //End of exercice : should ring three times
                    if (RUN) bell.ringsEnd();

                //reset controls before stopping the thread
                tfWork.setString(formatTime(workTime));
                tfRest.setString(formatTime(restTime));
                tfLaps.setString(String.valueOf(laps));

                getFrmMain().removeCommand(cmdStop);
                getFrmMain().addCommand(cmdStart);

                }
            };
                counter.start();
            
    }
    /*
        private void alert(int times, int duration) {
        for (int i=0; i < times; i++)
            //AlertType.INFO.playSound(getDisplay());
	new com.nokia.mid.sound.Sound(cfg.FREQ, duration).play(times);
         
        if (cfg.VIBRATE) getDisplay().vibrate(500);        
        if (cfg.FLASH)  getDisplay().flashBacklight(500);
    }
*/

}
